home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxclmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  38.8 KB  |  1,130 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxclmem.c,v 1.2 2000/09/19 19:00:34 lpd Exp $ */
  20. /* RAM-based command list implementation */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxclmem.h"
  25.  
  26. /*
  27.  * Based on: memfile.c        Version: 1.4 3/21/95 14:59:33 by Ray Johnston.
  28.  * Copyright assigned to Aladdin Enterprises.
  29.  */
  30.  
  31. /*****************************************************************************
  32.  
  33.    This package is more or less optimal for use by the clist routines, with
  34.    a couple of the more likely to change "tuning" parameters given in the
  35.    two macros below -- NEED_TO_COMPRESS and GET_NUM_RAW_BUFFERS. Usually
  36.    the NEED_TO_COMPRESS decision will be deferred as long as possible based
  37.    on some total system free RAM space remaining.
  38.  
  39.    The data structures are in "memfile.h", and the primary 'tuning' parameter
  40.    is MEMFILE_DATA_SIZE. This should not be too small to keep the overhead
  41.    ratio of the block structures to the clist data small. A value of 16384
  42.    is probably in the ballpark.
  43.  
  44.    The concept is that a memory based "file" is created initially without
  45.    compression, with index blocks every MEMFILE_DATA_SIZE of the file. The
  46.    primary blocks (used by the memfile_fseek logic) for indexing into the
  47.    file are called 'logical' (LOG_MEMFILE_BLK) and the data in stored in a
  48.    different block called a 'physical' block (PHYS_MEMFILE_BLK). When the
  49.    file is not yet compressed, indicated by (f->phys_curr==NULL), then there
  50.    is one physical block for each logical block. The physical block also has
  51.    the 'data_limit' set to NULL if the data is not compressed. Thus when a
  52.    file is not compressed there is one physical block for each logical block.
  53.  
  54. COMPRESSION.
  55.  
  56.    When compression is triggered for a file then all of the blocks except
  57.    the last are compressed.  Compression will result in a physical block
  58.    that holds data for more than one logical block. Each logical block now
  59.    points to the start of compressed data in a physical block with the
  60.    'phys_pdata' pointer. The 'data_limit' pointer in the physical block is
  61.    where the compression logic stopped storing data (as stream data
  62.    compressors are allowed to do). The data for the logical block may span
  63.    to the next physical block. Once physical blocks are compressed, they are
  64.    chained together using the 'link' field.
  65.  
  66.    The 'f->phys_curr' points to the block being filled by compression, with
  67.    the 'f->wt.ptr' pointing to the last byte filled in the block. These are
  68.    used during subsequent compression when the last logical block of the
  69.    file fills the physical block.
  70.  
  71. DECOMPRESSION.
  72.  
  73.    During reading the clist, if the logical block points to an uncompressed
  74.    physical block, then 'memfile_get_pdata' simply sets the 'pdata' and the
  75.    'pdata_end' pointers. If the logical block was compressed, then it may
  76.    still be resident in a cache of decompression buffers. The number of these
  77.    decompression buffers is not critical -- even one is enough, but having
  78.    more may prevent decompressing blocks more than once (a cache_miss). The
  79.    number of decompression buffers, called "raw" buffers, that are attempted
  80.    to allocate can be changed with the GET_NUM_RAW_BUFFERS macro, but no
  81.    error occurs if less than that number can be allocated.
  82.  
  83.    If the logical block still resides in a decompression cache buffer, then
  84.    the 'raw_block' will identify the block. If the data for a logical block
  85.    only exists in compressed form, then the "tail" of the list of decompression
  86.    buffers is re-used, marking the 'raw_block' of the logical block that was
  87.    previously associated with this data to NULL.
  88.  
  89.    Whichever raw decompression buffer is accessed is moved to the head of the
  90.    decompression buffer list in order to keep the tail of the list as the
  91.    "least recently used".
  92.  
  93.    There are some DEBUG global static variables used to count the number of
  94.    cache hits "tot_cache_hits" and the number of times a logical block is
  95.    decompressed "tot_cache_miss". Note that the actual number of cache miss
  96.    events is 'f->log_length/MEMFILE_DATA_SIZE - tot_cache_miss' since we
  97.    assume that every logical block must be decmpressed at least once.
  98.  
  99.    Empirical results so far indicate that if one cache raw buffer for every
  100.    32 logical blocks, then the hit/miss ratio exceeds 99%. Of course, the
  101.    number of raw buffers should be more than 1 if possible, and in many
  102.    implementations (single threaded), the memory usage does not increase
  103.    during the page output step so almost all of memory can be used for
  104.    these raw buffers to prevent the likelihood of a cache miss.
  105.  
  106.    Of course, this is dependent on reasonably efficient clist blocking
  107.    during writing which is dependent on the data and on the BufferSpace
  108.    value which determines the number of clist band data buffers available.
  109.    Empirical testing shows that the overall efficiency is best if the
  110.    BufferSpace value is 1,000,000 (as in the original Ghostscript source).
  111.    [Note: I expected to be able to use smaller buffer sizes for some cases,
  112.     but this resulted in a high level of thrashing...RJJ]
  113.  
  114. LIMITATIONS.
  115.  
  116.    The most serious limitation is caused by the way 'memfile_fwrite' decides
  117.    to free up and re-initialize a file. If memfile_fwrite is called after
  118.    a seek to any location except the start of the file, then an error is
  119.    issued since logic is not present to properly free up on a partial file.
  120.    This is not a problem as used by the 'clist' logic since rewind is used
  121.    to position to the start of a file when re-using it after an 'erasepage'.
  122.  
  123.    Since the 'clist' logic always traverses the clist using fseek's to ever
  124.    increasing locations, no optimizations of backward seeks was implemented.
  125.    This would be relatively easy with back chain links or bi-directional
  126.    "X-OR" pointer information to link the logical block chain. The rewind
  127.    function is optimal and moves directly to the start of the file.
  128.  
  129. ********************************************************************************/
  130.  
  131. /*
  132.    The need to compress should be conditional on the amount of available
  133.    memory, but we don't have a way to communicate this to these routines.
  134.    Instead, we simply start compressing when we've allocated more than
  135.    COMPRESSION_THRESHOLD amount of data.  The threshold should be at
  136.    least as large as the fixed overhead of the compressor plus the
  137.    decompressor, plus the expected compressed size of a block that size.
  138.  */
  139. private const long COMPRESSION_THRESHOLD = 300000;
  140.  
  141. #define NEED_TO_COMPRESS(f)\
  142.   ((f)->ok_to_compress && (f)->total_space > COMPRESSION_THRESHOLD)
  143.  
  144.    /* FOR NOW ALLOCATE 1 raw buffer for every 32 blocks (at least 8)    */
  145. #define GET_NUM_RAW_BUFFERS( f )                     \
  146.      max(f->log_length/MEMFILE_DATA_SIZE/32, 8)
  147.  
  148. #define MALLOC(f, siz, cname)\
  149.   (void *)gs_alloc_bytes((f)->data_memory, siz, cname)
  150. #define FREE(f, obj, cname)\
  151.   (gs_free_object((f)->data_memory, obj, cname),\
  152.    (f)->total_space -= sizeof(*(obj)))
  153.  
  154. /* Structure descriptor for GC */
  155. private_st_MEMFILE();
  156.  
  157.     /* forward references */
  158. private void memfile_free_mem(P1(MEMFILE * f));
  159. private int memfile_init_empty(P1(MEMFILE * f));
  160.  
  161. /************************************************/
  162. /*   #define DEBUG      /- force statistics -/  */
  163. /************************************************/
  164.  
  165. #ifdef DEBUG
  166. long tot_compressed;
  167. long tot_raw;
  168. long tot_cache_miss;
  169. long tot_cache_hits;
  170. long tot_swap_out;
  171.  
  172. /*
  173.    The following pointers are here only for helping with a dumb debugger
  174.    that can't inspect local variables!
  175.  */
  176. byte *decomp_wt_ptr0, *decomp_wt_limit0;
  177. const byte *decomp_rd_ptr0, *decomp_rd_limit0;
  178. byte *decomp_wt_ptr1, *decomp_wt_limit1;
  179. const byte *decomp_rd_ptr1, *decomp_rd_limit1;
  180.  
  181. #endif
  182.  
  183. /* ----------------------------- Memory Allocation --------------------- */
  184. void *    /* allocated memory's address, 0 if failure */
  185. allocateWithReserve(
  186.          MEMFILE  *f,            /* file to allocate mem to */
  187.          int      sizeofBlock,        /* size of block to allocate */
  188.          int      *return_code,         /* RET 0 ok, -ve GS-style error, or +1 if OK but low memory */
  189.      const   char     *allocName,        /* name to allocate by */
  190.      const   char     *errorMessage         /* error message to print */
  191. )
  192. {
  193.     int code = 0;    /* assume success */
  194.     void *block = MALLOC(f, sizeofBlock, allocName);
  195.  
  196.     if (block == NULL) {
  197.     /* Try to recover block from reserve */
  198.     if (sizeofBlock == sizeof(LOG_MEMFILE_BLK)) {
  199.         if (f->reserveLogBlockCount > 0) {
  200.         block = f->reserveLogBlockChain;
  201.         f->reserveLogBlockChain = f->reserveLogBlockChain->link;
  202.         --f->reserveLogBlockCount;
  203.         }
  204.     } else if (sizeofBlock == sizeof(PHYS_MEMFILE_BLK) ||
  205.            sizeofBlock == sizeof(RAW_BUFFER)
  206.            ) {
  207.         if (f->reservePhysBlockCount > 0) {
  208.         block = f->reservePhysBlockChain;
  209.         f->reservePhysBlockChain = f->reservePhysBlockChain->link;
  210.         --f->reservePhysBlockCount;
  211.         }
  212.     }
  213.     if (block != NULL)
  214.         code = 1;    /* successful, but allocated from reserve */
  215.     }
  216.     if (block != NULL)
  217.     f->total_space += sizeofBlock;
  218.     else
  219.     code = gs_note_error(gs_error_VMerror);
  220.     *return_code = code;
  221.     return block;
  222. }   
  223.  
  224. /* ---------------- Open/close/unlink ---------------- */
  225.  
  226. int
  227. memfile_fopen(char fname[gp_file_name_sizeof], const char *fmode,
  228.           clist_file_ptr /*MEMFILE * */  * pf,
  229.           gs_memory_t *mem, gs_memory_t *data_mem, bool ok_to_compress)
  230. {
  231.     MEMFILE *f = 0;
  232.     int code = 0;
  233.  
  234.     /* We don't implement reopening an existing file. */
  235.     if (fname[0] != 0 || fmode[0] != 'w') {
  236.     code = gs_note_error(gs_error_invalidfileaccess);
  237.     goto finish;
  238.     }
  239.  
  240.     /* There is no need to set fname in this implementation, */
  241.     /* but we do it anyway. */
  242.     fname[0] = (ok_to_compress ? 'a' : 'b');
  243.     fname[1] = 0;
  244.  
  245.     f = gs_alloc_struct(mem, MEMFILE, &st_MEMFILE,
  246.             "memfile_open_scratch(MEMFILE)");
  247.     if (f == NULL) {
  248.     eprintf1("memfile_open_scratch(%s): gs_alloc_struct failed\n", fname);
  249.     code = gs_note_error(gs_error_VMerror);
  250.     goto finish;
  251.     }
  252.     f->memory = mem;
  253.     f->data_memory = data_mem;
  254.     /* init an empty file, BEFORE allocating de/compress state */
  255.     f->compress_state = 0;    /* make clean for GC, or alloc'n failure */
  256.     f->decompress_state = 0;
  257.     f->total_space = 0;
  258.     f->reservePhysBlockChain = NULL;
  259.     f->reservePhysBlockCount = 0;
  260.     f->reserveLogBlockChain = NULL;
  261.     f->reserveLogBlockCount = 0;
  262.     /* init an empty file           */
  263.     if ((code = memfile_init_empty(f)) < 0)
  264.     goto finish;
  265.     if ((code = memfile_set_memory_warning(f, 0)) < 0)
  266.     goto finish;
  267.     /*
  268.      * Disregard the ok_to_compress flag, since the size threshold gives us
  269.      * a much better criterion for deciding when compression is appropriate.
  270.      */
  271.     f->ok_to_compress = /*ok_to_compress */ true;
  272.     f->compress_state = 0;    /* make clean for GC */
  273.     f->decompress_state = 0;
  274.     if (f->ok_to_compress) {
  275.     const stream_state *compress_proto = clist_compressor_state(NULL);
  276.     const stream_state *decompress_proto = clist_decompressor_state(NULL);
  277.     const stream_template *compress_template = compress_proto->template;
  278.     const stream_template *decompress_template = decompress_proto->template;
  279.  
  280.     f->compress_state =
  281.         gs_alloc_struct(mem, stream_state, compress_template->stype,
  282.                 "memfile_open_scratch(compress_state)");
  283.     f->decompress_state =
  284.         gs_alloc_struct(mem, stream_state, decompress_template->stype,
  285.                 "memfile_open_scratch(decompress_state)");
  286.     if (f->compress_state == 0 || f->decompress_state == 0) {
  287.         eprintf1("memfile_open_scratch(%s): gs_alloc_struct failed\n", fname);
  288.         code = gs_note_error(gs_error_VMerror);
  289.         goto finish;
  290.     }
  291.     memcpy(f->compress_state, compress_proto,
  292.            gs_struct_type_size(compress_template->stype));
  293.     f->compress_state->memory = mem;
  294.     memcpy(f->decompress_state, decompress_proto,
  295.            gs_struct_type_size(decompress_template->stype));
  296.     f->decompress_state->memory = mem;
  297.     if (compress_template->set_defaults)
  298.         (*compress_template->set_defaults) (f->compress_state);
  299.     if (decompress_template->set_defaults)
  300.         (*decompress_template->set_defaults) (f->decompress_state);
  301.     }
  302.     f->total_space = 0;
  303.  
  304. #ifdef DEBUG
  305.     /* If this is the start, init some statistics.       */
  306.     /* Hack: we know the 'a' file is opened first. */
  307.     if (*fname == 'a') {
  308.     tot_compressed = 0;
  309.     tot_raw = 0;
  310.     tot_cache_miss = 0;
  311.     tot_cache_hits = 0;
  312.     tot_swap_out = 0;
  313.     }
  314. #endif
  315. finish:
  316.     if (code < 0) {
  317.     /* return failure, clean up memory before leaving */
  318.     if (f != NULL)
  319.         memfile_fclose((clist_file_ptr)f, fname, true);
  320.     } else {
  321.       /* return success */
  322.       *pf = f;
  323.     }
  324.     return code;
  325. }
  326.  
  327. int
  328. memfile_fclose(clist_file_ptr cf, const char *fname, bool delete)
  329. {
  330.     MEMFILE *const f = (MEMFILE *)cf;
  331.  
  332.     /* We don't implement closing without deletion. */
  333.     if (!delete)
  334.     return_error(gs_error_invalidfileaccess);
  335.     memfile_free_mem(f);
  336.  
  337.     /* Free reserve blocks; don't do it in memfile_free_mem because */
  338.     /* that routine gets called to reinit file */
  339.     while (f->reserveLogBlockChain != NULL) {
  340.     LOG_MEMFILE_BLK *block = f->reserveLogBlockChain;
  341.  
  342.     f->reserveLogBlockChain = block->link;
  343.     FREE(f, block, "memfile_set_block_size");
  344.     }
  345.     while (f->reservePhysBlockChain != NULL) {
  346.     PHYS_MEMFILE_BLK *block = f->reservePhysBlockChain;
  347.  
  348.     f->reservePhysBlockChain = block->link;
  349.     FREE(f, block, "memfile_set_block_size");
  350.     }
  351.  
  352.     /* deallocate de/compress state */
  353.     gs_free_object(f->memory, f->decompress_state,
  354.            "memfile_close_and_unlink(decompress_state)");
  355.     gs_free_object(f->memory, f->compress_state,
  356.            "memfile_close_and_unlink(compress_state)");
  357.  
  358.     /* deallocate the memfile object proper */
  359.     gs_free_object(f->memory, f, "memfile_close_and_unlink(MEMFILE)");
  360.     return 0;
  361. }
  362.  
  363. int
  364. memfile_unlink(const char *fname)
  365. {
  366.     /*
  367.      * Since we have no way to represent a memfile other than by the
  368.      * pointer, we don't (can't) implement unlinking.
  369.      */
  370.     return_error(gs_error_invalidfileaccess);
  371. }
  372.  
  373. /* ---------------- Writing ---------------- */
  374.  
  375. /* Pre-alloc enough reserve mem blox to guarantee a write of N bytes will succeed */
  376. int    /* returns 0 ok, gs_error_VMerror if insufficient */
  377. memfile_set_memory_warning(clist_file_ptr cf, int bytes_left)
  378. {
  379.     MEMFILE *const f = (MEMFILE *)cf;
  380.     int code = 0;
  381.     /*
  382.      * Determine req'd memory block count from bytes_left.
  383.      * Allocate enough phys & log blocks to hold bytes_left
  384.      * + 1 phys blk for compress_log_blk + 1 phys blk for decompress.
  385.      */
  386.     int logNeeded =
  387.     (bytes_left + MEMFILE_DATA_SIZE - 1) / MEMFILE_DATA_SIZE;
  388.     int physNeeded = logNeeded;
  389.  
  390.     if (bytes_left > 0)
  391.     ++physNeeded;
  392.     if (f->raw_head == NULL)
  393.     ++physNeeded;    /* have yet to allocate read buffers */
  394.  
  395.     /* Allocate or free memory depending on need */
  396.     while (logNeeded > f->reserveLogBlockCount) {
  397.     LOG_MEMFILE_BLK *block =
  398.         MALLOC( f, sizeof(LOG_MEMFILE_BLK), "memfile_set_block_size" );
  399.  
  400.     if (block == NULL) {
  401.         code = gs_note_error(gs_error_VMerror);
  402.         goto finish;
  403.     }
  404.     block->link = f->reserveLogBlockChain;
  405.     f->reserveLogBlockChain = block;
  406.     ++f->reserveLogBlockCount;
  407.     }
  408.     while (logNeeded < f->reserveLogBlockCount) {
  409.     LOG_MEMFILE_BLK *block = f->reserveLogBlockChain;
  410.  
  411.     f->reserveLogBlockChain = block->link;
  412.     FREE(f, block, "memfile_set_block_size");
  413.     --f->reserveLogBlockCount;
  414.     }
  415.     while (physNeeded > f->reservePhysBlockCount) {
  416.     PHYS_MEMFILE_BLK *block =
  417.         MALLOC( f,
  418.             max( sizeof(PHYS_MEMFILE_BLK), sizeof(RAW_BUFFER) ),
  419.             "memfile_set_block_size");
  420.  
  421.     if (block == NULL) {
  422.         code = gs_note_error(gs_error_VMerror);
  423.         goto finish;
  424.     }
  425.     block->link = f->reservePhysBlockChain;
  426.     f->reservePhysBlockChain = block;
  427.     ++f->reservePhysBlockCount;
  428.     }
  429.     while (physNeeded < f->reservePhysBlockCount) {
  430.     PHYS_MEMFILE_BLK *block = f->reservePhysBlockChain;
  431.  
  432.     f->reservePhysBlockChain = block->link;
  433.     FREE(f, block, "memfile_set_block_size");
  434.     --f->reservePhysBlockCount;
  435.     }
  436.     f->error_code = 0;    /* memfile_set_block_size is how user resets this */
  437. finish:
  438.     return code;
  439. }
  440.  
  441. private int
  442. compress_log_blk(MEMFILE * f, LOG_MEMFILE_BLK * bp)
  443. {
  444.     int status;
  445.     int ecode = 0;        /* accumulate low-memory warnings */
  446.     int code;
  447.     long compressed_size;
  448.     byte *start_ptr;
  449.     PHYS_MEMFILE_BLK *newphys;
  450.  
  451.     /* compress this block */
  452.     f->rd.ptr = (const byte *)(bp->phys_blk->data) - 1;
  453.     f->rd.limit = f->rd.ptr + MEMFILE_DATA_SIZE;
  454.  
  455.     bp->phys_blk = f->phys_curr;
  456.     bp->phys_pdata = (char *)(f->wt.ptr) + 1;
  457.     if (f->compress_state->template->reinit != 0)
  458.     (*f->compress_state->template->reinit)(f->compress_state);
  459.     compressed_size = 0;
  460.  
  461.     start_ptr = f->wt.ptr;
  462.     status = (*f->compress_state->template->process)(f->compress_state,
  463.                              &(f->rd), &(f->wt), true);
  464.     bp->phys_blk->data_limit = (char *)(f->wt.ptr);
  465.  
  466.     if (status == 1) {        /* More output space needed (see strimpl.h) */
  467.     /* allocate another physical block, then compress remainder       */
  468.     compressed_size = f->wt.limit - start_ptr;
  469.     newphys =
  470.         allocateWithReserve(f, sizeof(*newphys), &code, "memfile newphys",
  471.             "compress_log_blk : MALLOC for 'newphys' failed\n");
  472.     if (code < 0)
  473.         return code;
  474.     ecode |= code;    /* accumulate any low-memory warnings */
  475.     newphys->link = NULL;
  476.     bp->phys_blk->link = newphys;
  477.     f->phys_curr = newphys;
  478.     f->wt.ptr = (byte *) (newphys->data) - 1;
  479.     f->wt.limit = f->wt.ptr + MEMFILE_DATA_SIZE;
  480.  
  481.     start_ptr = f->wt.ptr;
  482.     status =
  483.         (*f->compress_state->template->process)(f->compress_state,
  484.                             &(f->rd), &(f->wt), true);
  485.     if (status != 0) {
  486.         /*
  487.          * You'd think the above line is a bug, but in real life 1 src
  488.          * block never ends up getting split across 3 dest blocks.
  489.          */
  490.         /* CHANGE memfile_set_memory_warning if this assumption changes. */
  491.         eprintf("Compression required more than one full block!\n");
  492.         return_error(gs_error_Fatal);
  493.     }
  494.     newphys->data_limit = (char *)(f->wt.ptr);
  495.     }
  496.     compressed_size += f->wt.ptr - start_ptr;
  497.     if (compressed_size > MEMFILE_DATA_SIZE) {
  498.     eprintf2("\nCompression didn't - raw=%d, compressed=%ld\n",
  499.          MEMFILE_DATA_SIZE, compressed_size);
  500.     }
  501. #ifdef DEBUG
  502.     tot_compressed += compressed_size;
  503. #endif
  504.     return (status < 0 ? gs_note_error(gs_error_ioerror) : ecode);
  505. }                /* end "compress_log_blk()"                                     */
  506.  
  507. /*      Internal (private) routine to handle end of logical block       */
  508. private int    /* ret 0 ok, -ve error, or +ve low-memory warning */
  509. memfile_next_blk(MEMFILE * f)
  510. {
  511.     LOG_MEMFILE_BLK *bp = f->log_curr_blk;
  512.     LOG_MEMFILE_BLK *newbp;
  513.     PHYS_MEMFILE_BLK *newphys, *oldphys;
  514.     int ecode = 0;        /* accumulate low-memory warnings */
  515.     int code;
  516.  
  517.     if (f->phys_curr == NULL) {    /* means NOT compressing                */
  518.     /* allocate a new block                                           */
  519.     newphys =
  520.         allocateWithReserve(f, sizeof(*newphys), &code, "memfile newphys",
  521.             "memfile_next_blk: MALLOC 1 for 'newphys' failed\n");
  522.     if (code < 0)
  523.         return code;
  524.     ecode |= code;    /* accumulate low-mem warnings */
  525.     newphys->link = NULL;
  526.     newphys->data_limit = NULL;    /* raw                          */
  527.  
  528.     newbp =
  529.         allocateWithReserve(f, sizeof(*newbp), &code, "memfile newbp",
  530.             "memfile_next_blk: MALLOC 1 for 'newbp' failed\n");
  531.     if (code < 0) {
  532.         FREE(f, newphys, "memfile newphys");
  533.         return code;
  534.     }
  535.     ecode |= code;    /* accumulate low-mem warnings */
  536.     bp->link = newbp;
  537.     newbp->link = NULL;
  538.     newbp->raw_block = NULL;
  539.     f->log_curr_blk = newbp;
  540.  
  541.     /* check if need to start compressing                             */
  542.     if (NEED_TO_COMPRESS(f)) {
  543.         if_debug0(':', "[:]Beginning compression\n");
  544.         /* compress the entire file up to this point                   */
  545.         if (!f->compressor_initialized) {
  546.         int code = 0;
  547.  
  548.         if (f->compress_state->template->init != 0)
  549.             code = (*f->compress_state->template->init) (f->compress_state);
  550.         if (code < 0)
  551.             return_error(gs_error_VMerror);  /****** BOGUS ******/
  552.         if (f->decompress_state->template->init != 0)
  553.             code = (*f->decompress_state->template->init)
  554.             (f->decompress_state);
  555.         if (code < 0)
  556.             return_error(gs_error_VMerror);  /****** BOGUS ******/
  557.         f->compressor_initialized = true;
  558.         }
  559.         /* Write into the new physical block we just allocated,        */
  560.         /* replace it after the loop (after some blocks are freed)     */
  561.         f->phys_curr = newphys;
  562.         f->wt.ptr = (byte *) (newphys->data) - 1;
  563.         f->wt.limit = f->wt.ptr + MEMFILE_DATA_SIZE;
  564.         bp = f->log_head;
  565.         while (bp != newbp) {    /* don't compress last block    */
  566.         int code;
  567.  
  568.         oldphys = bp->phys_blk;
  569.         if ((code = compress_log_blk(f, bp)) < 0)
  570.             return code;
  571.         ecode |= code;
  572.         FREE(f, oldphys, "memfile_next_blk(oldphys)");
  573.         bp = bp->link;
  574.         }            /* end while( ) compress loop                           */
  575.         /* Allocate a physical block for this (last) logical block     */
  576.         newphys =
  577.         allocateWithReserve(f, sizeof(*newphys), &code,
  578.             "memfile newphys",
  579.             "memfile_next_blk: MALLOC 2 for 'newphys' failed\n");
  580.         if (code < 0)
  581.         return code;
  582.         ecode |= code;    /* accumulate low-mem warnings */
  583.         newphys->link = NULL;
  584.         newphys->data_limit = NULL;        /* raw                  */
  585.  
  586.     }            /* end convert file to compressed                                 */
  587.     newbp->phys_blk = newphys;
  588.     f->pdata = newphys->data;
  589.     f->pdata_end = newphys->data + MEMFILE_DATA_SIZE;
  590.     }    /* end if NOT compressing                                 */
  591.     /* File IS being compressed                                       */ 
  592.     else {
  593.     int code;
  594.  
  595.     oldphys = bp->phys_blk;    /* save raw phys block ID               */
  596.     /* compresses bp on phys list  */
  597.     if ((code = compress_log_blk(f, bp)) < 0)
  598.         return code;
  599.     ecode |= code;
  600.     newbp =
  601.         allocateWithReserve(f, sizeof(*newbp), &code, "memfile newbp",
  602.             "memfile_next_blk: MALLOC 2 for 'newbp' failed\n");
  603.     if (code < 0)
  604.         return code;
  605.     ecode |= code;
  606.     bp->link = newbp;
  607.     newbp->link = NULL;
  608.     newbp->raw_block = NULL;
  609.     /* Re-use the raw phys block for this new logical blk             */
  610.     newbp->phys_blk = oldphys;
  611.     f->pdata = oldphys->data;
  612.     f->pdata_end = f->pdata + MEMFILE_DATA_SIZE;
  613.     f->log_curr_blk = newbp;
  614.     }                /* end else (when we are compressing)                           */
  615.  
  616.     return (ecode);
  617. }
  618.  
  619. int    /* returns # of chars actually written */
  620. memfile_fwrite_chars(const void *data, uint len, clist_file_ptr cf)
  621. {
  622.     const char *str = (const char *)data;
  623.     MEMFILE *f = (MEMFILE *) cf;
  624.     uint count = len;
  625.     int ecode;
  626.  
  627.     /* check if we are writing to the start of the file.  If so, then    */
  628.     /* free the file memory and re-initialize it (frees memory)          */
  629.     if (f->log_curr_pos == 0) {
  630.     int code;
  631.  
  632.     memfile_free_mem(f);
  633.     if ((code = memfile_init_empty(f)) < 0) {
  634.         f->error_code = code;
  635.         return 0;
  636.     }
  637.     }
  638.     if (f->log_curr_blk->link != 0) {
  639.     eprintf(" Write file truncate -- need to free physical blocks.\n");
  640.     }
  641.     while (count) {
  642.     uint move_count = f->pdata_end - f->pdata;
  643.  
  644.     if (move_count == 0) {
  645.         if ((ecode = memfile_next_blk(f)) != 0) {
  646.         f->error_code = ecode;
  647.         if (ecode < 0)
  648.             return 0;
  649.         }
  650.     } else {
  651.         if (move_count > count)
  652.         move_count = count;
  653.         memmove(f->pdata, str, move_count);
  654.         f->pdata += move_count;
  655.         str += move_count;
  656.         count -= move_count;
  657.     }
  658.     }
  659.     f->log_curr_pos += len;
  660.     f->log_length = f->log_curr_pos;    /* truncate length to here      */
  661. #ifdef DEBUG
  662.     tot_raw += len;
  663. #endif
  664.     return (len);
  665. }
  666.  
  667. /*                                                                      */
  668. /*      Internal routine to set the f->pdata and f->pdata_end pointers  */
  669. /*      for the current logical block f->log_curr_blk                   */
  670. /*                                                                      */
  671. /*      If data only exists in compressed form, allocate a raw buffer   */
  672. /*      and decompress it.                                              */
  673. /*                                                                      */
  674.  
  675. private int
  676. memfile_get_pdata(MEMFILE * f)
  677. {
  678.     int i, num_raw_buffers, status;
  679.     LOG_MEMFILE_BLK *bp = f->log_curr_blk;
  680.  
  681.     if (bp->phys_blk->data_limit == NULL) {
  682.     /* Not compressed, return this data pointer                       */
  683.     f->pdata = (bp->phys_blk)->data;
  684.     i = f->log_curr_pos % MEMFILE_DATA_SIZE;    /* pos within block     */
  685.     i = f->log_curr_pos - i;    /* base of block        */
  686.     if (i + MEMFILE_DATA_SIZE > f->log_length)
  687.         f->pdata_end = f->pdata + f->log_length - i;
  688.     else
  689.         f->pdata_end = f->pdata + MEMFILE_DATA_SIZE;
  690.     } else {
  691.     /* data was compressed                                            */
  692.     if (f->raw_head == NULL) {
  693.         /* need to allocate the raw buffer pool                        */
  694.         num_raw_buffers = GET_NUM_RAW_BUFFERS(f);
  695.         if (f->reservePhysBlockCount) {
  696.             /* HACK: allocate reserve block that's been reserved for
  697.          * decompression.  This buffer's block was pre-allocated to make
  698.          * sure we won't come up short here. Take from chain instead of
  699.          * allocateWithReserve() since this buf would just be wasted if
  700.          * allowed to remain preallocated. */
  701.         f->raw_head = (RAW_BUFFER *)f->reservePhysBlockChain;
  702.         f->reservePhysBlockChain = f->reservePhysBlockChain->link;
  703.         --f->reservePhysBlockCount;
  704.         } else {
  705.         int code;
  706.  
  707.         f->raw_head =
  708.             allocateWithReserve(f, sizeof(*f->raw_head), &code,
  709.                     "memfile raw buffer",
  710.             "memfile_get_pdata: MALLOC for 'raw_head' failed\n");
  711.         if (code < 0)
  712.             return code;
  713.         }
  714.         f->raw_head->back = NULL;
  715.         f->raw_tail = f->raw_head;
  716.         f->raw_tail->log_blk = NULL;
  717.         for (i = 0; i < num_raw_buffers; i++) {
  718.         f->raw_tail->fwd = (RAW_BUFFER *) MALLOC(f, sizeof(RAW_BUFFER),
  719.                               "memfile raw buffer");
  720.         /* if MALLOC fails, then just stop allocating            */
  721.         if (!f->raw_tail->fwd)
  722.             break;
  723.         f->total_space += sizeof(RAW_BUFFER);
  724.         f->raw_tail->fwd->back = f->raw_tail;
  725.         f->raw_tail = f->raw_tail->fwd;
  726.         f->raw_tail->log_blk = NULL;
  727.         }
  728.         f->raw_tail->fwd = NULL;
  729.         num_raw_buffers = i + 1;    /* if MALLOC failed, then OK    */
  730.         if_debug1(':', "[:]Number of raw buffers allocated=%d\n",
  731.               num_raw_buffers);
  732.     }            /* end allocating the raw buffer pool (first time only)           */
  733.     if (bp->raw_block == NULL) {
  734. #ifdef DEBUG
  735.         tot_cache_miss++;    /* count every decompress       */
  736. #endif
  737.         /* find a raw buffer and decompress                            */
  738.         if (f->raw_tail->log_blk != NULL) {
  739.         /* This block was in use, grab it                           */
  740. #ifdef DEBUG
  741.         tot_swap_out++;
  742. #endif
  743.         f->raw_tail->log_blk->raw_block = NULL;        /* data no longer here */
  744.         f->raw_tail->log_blk = NULL;
  745.         }
  746.         /* Use the last raw block in the chain (the oldest)            */
  747.         f->raw_tail->back->fwd = NULL;    /* disconnect from tail */
  748.         f->raw_tail->fwd = f->raw_head;    /* new head             */
  749.         f->raw_head->back = f->raw_tail;
  750.         f->raw_tail = f->raw_tail->back;
  751.         f->raw_head = f->raw_head->back;
  752.         f->raw_head->back = NULL;
  753.         f->raw_head->log_blk = bp;
  754.  
  755.         /* Decompress the data into this raw block                     */
  756.         /* Initialize the decompressor                              */
  757.         if (f->decompress_state->template->reinit != 0)
  758.         (*f->decompress_state->template->reinit) (f->decompress_state);
  759.         /* Set pointers and call the decompress routine             */
  760.         f->wt.ptr = (byte *) (f->raw_head->data) - 1;
  761.         f->wt.limit = f->wt.ptr + MEMFILE_DATA_SIZE;
  762.         f->rd.ptr = (const byte *)(bp->phys_pdata) - 1;
  763.         f->rd.limit = (const byte *)bp->phys_blk->data_limit;
  764. #ifdef DEBUG
  765.         decomp_wt_ptr0 = f->wt.ptr;
  766.         decomp_wt_limit0 = f->wt.limit;
  767.         decomp_rd_ptr0 = f->rd.ptr;
  768.         decomp_rd_limit0 = f->rd.limit;
  769. #endif
  770.         status = (*f->decompress_state->template->process)
  771.         (f->decompress_state, &(f->rd), &(f->wt), true);
  772.         if (status == 0) {    /* More input data needed */
  773.         /* switch to next block and continue decompress             */
  774.         int back_up = 0;    /* adjust pointer backwards     */
  775.  
  776.         if (f->rd.ptr != f->rd.limit) {
  777.             /* transfer remainder bytes from the previous block      */
  778.             back_up = f->rd.limit - f->rd.ptr;
  779.             for (i = 0; i < back_up; i++)
  780.             *(bp->phys_blk->link->data - back_up + i) = *++f->rd.ptr;
  781.         }
  782.         f->rd.ptr = (const byte *)bp->phys_blk->link->data - back_up - 1;
  783.         f->rd.limit = (const byte *)bp->phys_blk->link->data_limit;
  784. #ifdef DEBUG
  785.         decomp_wt_ptr1 = f->wt.ptr;
  786.         decomp_wt_limit1 = f->wt.limit;
  787.         decomp_rd_ptr1 = f->rd.ptr;
  788.         decomp_rd_limit1 = f->rd.limit;
  789. #endif
  790.         status = (*f->decompress_state->template->process)
  791.             (f->decompress_state, &(f->rd), &(f->wt), true);
  792.         if (status == 0) {
  793.             eprintf("Decompression required more than one full block!\n");
  794.             return_error(gs_error_Fatal);
  795.         }
  796.         }
  797.         bp->raw_block = f->raw_head;    /* point to raw block           */
  798.     }
  799.     /* end if( raw_block == NULL ) meaning need to decompress data    */ 
  800.     else {
  801.         /* data exists in the raw data cache, if not raw_head, move it */
  802.         if (bp->raw_block != f->raw_head) {
  803.         /*          move to raw_head                                */
  804.         /*          prev.fwd = this.fwd                             */
  805.         bp->raw_block->back->fwd = bp->raw_block->fwd;
  806.         if (bp->raw_block->fwd != NULL)
  807.             /*               next.back = this.back                   */
  808.             bp->raw_block->fwd->back = bp->raw_block->back;
  809.         else
  810.             f->raw_tail = bp->raw_block->back;    /* tail = prev        */
  811.         f->raw_head->back = bp->raw_block;    /* head.back = this     */
  812.         bp->raw_block->fwd = f->raw_head;    /* this.fwd = orig head */
  813.         f->raw_head = bp->raw_block;    /* head = this          */
  814.         f->raw_head->back = NULL;    /* this.back = NULL     */
  815. #ifdef DEBUG
  816.         tot_cache_hits++;    /* counting here prevents repeats since */
  817.         /* won't count if already at head       */
  818. #endif
  819.         }
  820.     }
  821.     f->pdata = bp->raw_block->data;
  822.     f->pdata_end = f->pdata + MEMFILE_DATA_SIZE;
  823.     /* NOTE: last block is never compressed, so a compressed block    */
  824.     /*        is always full size.                                    */
  825.     }                /* end else (when data was compressed)                             */
  826.  
  827.     return (0);
  828. }
  829.  
  830. /* ---------------- Reading ---------------- */
  831.  
  832. int
  833. memfile_fread_chars(void *data, uint len, clist_file_ptr cf)
  834. {
  835.     char *str = (char *)data;
  836.     MEMFILE *f = (MEMFILE *) cf;
  837.     uint count = len, num_read, move_count;
  838.  
  839.     num_read = f->log_length - f->log_curr_pos;
  840.     if (count > num_read)
  841.     count = num_read;
  842.     num_read = count;
  843.  
  844.     while (count) {
  845.     f->log_curr_pos++;    /* move into next byte */
  846.     if (f->pdata == f->pdata_end) {
  847.         f->log_curr_blk = (f->log_curr_blk)->link;
  848.         memfile_get_pdata(f);
  849.     }
  850.     move_count = f->pdata_end - f->pdata;
  851.     if (move_count > count)
  852.         move_count = count;
  853.     f->log_curr_pos += move_count - 1;    /* new position         */
  854.     memmove(str, f->pdata, move_count);
  855.     str += move_count;
  856.     f->pdata += move_count;
  857.     count -= move_count;
  858.     }
  859.  
  860.     return (num_read);
  861. }
  862.  
  863. /* ---------------- Position/status ---------------- */
  864.  
  865. int
  866. memfile_ferror_code(clist_file_ptr cf)
  867. {
  868.     return (((MEMFILE *) cf)->error_code);    /* errors stored here */
  869. }
  870.  
  871. long
  872. memfile_ftell(clist_file_ptr cf)
  873. {
  874.     return (((MEMFILE *) cf)->log_curr_pos);
  875. }
  876.  
  877. void
  878. memfile_rewind(clist_file_ptr cf, bool discard_data, const char *ignore_fname)
  879. {
  880.     MEMFILE *f = (MEMFILE *) cf;
  881.  
  882.     if (discard_data) {
  883.     memfile_free_mem(f);
  884.     /* We have to call memfile_init_empty to preserve invariants. */
  885.     memfile_init_empty(f);
  886.     } else {
  887.     f->log_curr_blk = f->log_head;
  888.     f->log_curr_pos = 0;
  889.     memfile_get_pdata(f);
  890.     }
  891. }
  892.  
  893. int
  894. memfile_fseek(clist_file_ptr cf, long offset, int mode, const char *ignore_fname)
  895. {
  896.     MEMFILE *f = (MEMFILE *) cf;
  897.     long i, block_num, new_pos;
  898.  
  899.     switch (mode) {
  900.     case SEEK_SET:        /* offset from the beginning of the file */
  901.         new_pos = offset;
  902.         break;
  903.  
  904.     case SEEK_CUR:        /* offset from the current position in the file */
  905.         new_pos = offset + f->log_curr_pos;
  906.         break;
  907.  
  908.     case SEEK_END:        /* offset back from the end of the file */
  909.         new_pos = f->log_length - offset;
  910.         break;
  911.  
  912.     default:
  913.         return (-1);
  914.     }
  915.     if (new_pos < 0 || new_pos > f->log_length)
  916.     return -1;
  917.     if ((f->pdata == f->pdata_end) && (f->log_curr_blk->link != NULL)) {
  918.     /* log_curr_blk is actually one block behind log_curr_pos         */
  919.     f->log_curr_blk = f->log_curr_blk->link;
  920.     }
  921.     block_num = new_pos / MEMFILE_DATA_SIZE;
  922.     i = f->log_curr_pos / MEMFILE_DATA_SIZE;
  923.     if (block_num < i) {    /* if moving backwards, start at beginning */
  924.     f->log_curr_blk = f->log_head;
  925.     i = 0;
  926.     }
  927.     for (; i < block_num; i++) {
  928.     f->log_curr_blk = f->log_curr_blk->link;
  929.     }
  930.     f->log_curr_pos = new_pos;
  931.     memfile_get_pdata(f);    /* pointers to start of block           */
  932.     f->pdata += new_pos - (block_num * MEMFILE_DATA_SIZE);
  933.  
  934.     return 0;            /* return "normal" status                       */
  935. }
  936.  
  937. /* ---------------- Internal routines ---------------- */
  938.  
  939. private void
  940. memfile_free_mem(MEMFILE * f)
  941. {
  942.     LOG_MEMFILE_BLK *bp, *tmpbp;
  943.  
  944. #ifdef DEBUG
  945.     /* output some diagnostics about the effectiveness                   */
  946.     if (tot_raw > 100) {
  947.     if_debug2(':', "[:]tot_raw=%ld, tot_compressed=%ld\n",
  948.           tot_raw, tot_compressed);
  949.     }
  950.     if (tot_cache_hits != 0) {
  951.     if_debug3(':', "[:]Cache hits=%ld, cache misses=%ld, swapouts=%ld\n",
  952.          tot_cache_hits,
  953.          tot_cache_miss - (f->log_length / MEMFILE_DATA_SIZE),
  954.          tot_swap_out);
  955.     }
  956.     tot_raw = 0;
  957.     tot_compressed = 0;
  958.     tot_cache_hits = 0;
  959.     tot_cache_miss = 0;
  960.     tot_swap_out = 0;
  961. #endif
  962.  
  963.     /* Free up memory that was allocated for the memfile              */
  964.     bp = f->log_head;
  965.  
  966. /******************************************************************
  967.  * The following was the original algorithm here.  This algorithm has a bug:
  968.  * the second loop references the physical blocks again after they have been
  969.  * freed.
  970.  ******************************************************************/
  971.  
  972. #if 0                /**************** *************** */
  973.  
  974.     if (bp != NULL) {
  975.     /* Free the physical blocks that make up the compressed data      */
  976.     PHYS_MEMFILE_BLK *pphys = (f->log_head)->phys_blk;
  977.  
  978.     if (pphys->data_limit != NULL) {
  979.         /* the data was compressed, free the chain of blocks             */
  980.         while (pphys != NULL) {
  981.         PHYS_MEMFILE_BLK *tmpphys = pphys->link;
  982.  
  983.         FREE(f, pphys, "memfile_free_mem(pphys)");
  984.         pphys = tmpphys;
  985.         }
  986.     }
  987.     }
  988.     /* free the logical blocks                                        */
  989.     while (bp != NULL) {
  990.     /* if this logical block was not compressed, free the phys_blk  */
  991.     if (bp->phys_blk->data_limit == NULL) {
  992.         FREE(f, bp->phys_blk, "memfile_free_mem(phys_blk)");
  993.     }
  994.     tmpbp = bp->link;
  995.     FREE(f, bp, "memfile_free_mem(log_blk)");
  996.     bp = tmpbp;
  997.     }
  998.  
  999. #else /**************** *************** */
  1000. # if 1                /**************** *************** */
  1001.  
  1002. /****************************************************************
  1003.  * This algorithm is correct (we think).
  1004.  ****************************************************************/
  1005.  
  1006.     if (bp != NULL) {
  1007.     /* Null out phys_blk pointers to compressed data. */
  1008.     PHYS_MEMFILE_BLK *pphys = bp->phys_blk;
  1009.  
  1010.     {
  1011.         for (tmpbp = bp; tmpbp != NULL; tmpbp = tmpbp->link)
  1012.         if (tmpbp->phys_blk->data_limit != NULL)
  1013.             tmpbp->phys_blk = 0;
  1014.     }
  1015.     /* Free the physical blocks that make up the compressed data      */
  1016.     if (pphys->data_limit != NULL) {
  1017.         /* the data was compressed, free the chain of blocks             */
  1018.         while (pphys != NULL) {
  1019.         PHYS_MEMFILE_BLK *tmpphys = pphys->link;
  1020.  
  1021.         FREE(f, pphys, "memfile_free_mem(pphys)");
  1022.         pphys = tmpphys;
  1023.         }
  1024.     }
  1025.     }
  1026.     /* Now free the logical blocks, and any uncompressed physical blocks. */
  1027.     while (bp != NULL) {
  1028.     if (bp->phys_blk != NULL) {
  1029.         FREE(f, bp->phys_blk, "memfile_free_mem(phys_blk)");
  1030.     }
  1031.     tmpbp = bp->link;
  1032.     FREE(f, bp, "memfile_free_mem(log_blk)");
  1033.     bp = tmpbp;
  1034.     }
  1035.  
  1036. /***********************************************************************
  1037.  * This algorithm appears to be both simpler and free of the bug that
  1038.  * occasionally causes the older one to reference freed blocks; but in
  1039.  * fact it can miss blocks, because the very last compressed logical block
  1040.  * can have spill into a second physical block, which is not referenced by
  1041.  * any logical block.
  1042.  ***********************************************************************/
  1043.  
  1044. # else                /**************** *************** */
  1045.  
  1046.     {
  1047.     PHYS_MEMFILE_BLK *prev_phys = 0;
  1048.  
  1049.     while (bp != NULL) {
  1050.         PHYS_MEMFILE_BLK *phys = bp->phys_blk;
  1051.  
  1052.         if (phys != prev_phys) {
  1053.         FREE(f, phys, "memfile_free_mem(phys_blk)");
  1054.         prev_phys = phys;
  1055.         }
  1056.         tmpbp = bp->link;
  1057.         FREE(f, bp, "memfile_free_mem(log_blk)");
  1058.         bp = tmpbp;
  1059.     }
  1060.     }
  1061.  
  1062. # endif                /**************** *************** */
  1063. #endif /**************** *************** */
  1064.  
  1065.     f->log_head = NULL;
  1066.  
  1067.     /* Free any internal compressor state. */
  1068.     if (f->compressor_initialized) {
  1069.     if (f->decompress_state->template->release != 0)
  1070.         (*f->decompress_state->template->release) (f->decompress_state);
  1071.     if (f->compress_state->template->release != 0)
  1072.         (*f->compress_state->template->release) (f->compress_state);
  1073.     f->compressor_initialized = false;
  1074.     }
  1075.     /* free the raw buffers                                           */
  1076.     while (f->raw_head != NULL) {
  1077.     RAW_BUFFER *tmpraw = f->raw_head->fwd;
  1078.  
  1079.     FREE(f, f->raw_head, "memfile_free_mem(raw)");
  1080.     f->raw_head = tmpraw;
  1081.     }
  1082. }
  1083.  
  1084. private int
  1085. memfile_init_empty(MEMFILE * f)
  1086. {
  1087.     PHYS_MEMFILE_BLK *pphys;
  1088.     LOG_MEMFILE_BLK *plog;
  1089.  
  1090.    /* Zero out key fields so that allocation failure will be unwindable */
  1091.     f->phys_curr = NULL;     /* flag as file not compressed        */
  1092.     f->log_head = NULL;
  1093.     f->log_curr_blk = NULL;
  1094.     f->log_curr_pos = 0;
  1095.     f->log_length = 0;
  1096.     f->raw_head = NULL;
  1097.     f->compressor_initialized = false;
  1098.     f->total_space = 0;
  1099.  
  1100.     /* File empty - get a physical mem block (includes the buffer area)  */
  1101.     pphys = MALLOC(f, sizeof(*pphys), "memfile pphys");
  1102.     if (!pphys) {
  1103.     eprintf("memfile_init_empty: MALLOC for 'pphys' failed\n");
  1104.     return_error(gs_error_VMerror);
  1105.     }
  1106.     f->total_space += sizeof(*pphys);
  1107.     pphys->data_limit = NULL;    /* raw data for now     */
  1108.  
  1109.    /* Get logical mem block to go with physical one */
  1110.     plog = (LOG_MEMFILE_BLK *)MALLOC( f, sizeof(*plog), "memfile_init_empty" );
  1111.     if (plog == NULL) {
  1112.     FREE(f, pphys, "memfile_init_empty");
  1113.     eprintf("memfile_init_empty: MALLOC for log_curr_blk failed\n");
  1114.     return_error(gs_error_VMerror);
  1115.     }
  1116.     f->total_space += sizeof(*plog);
  1117.     f->log_head = f->log_curr_blk = plog;
  1118.     f->log_curr_blk->link = NULL;
  1119.     f->log_curr_blk->phys_blk = pphys;
  1120.     f->log_curr_blk->phys_pdata = NULL;
  1121.     f->log_curr_blk->raw_block = NULL;
  1122.  
  1123.     f->pdata = pphys->data;
  1124.     f->pdata_end = f->pdata + MEMFILE_DATA_SIZE;
  1125.  
  1126.     f->error_code = 0;
  1127.  
  1128.     return 0;
  1129. }
  1130.